package com.java.ai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

@RequestMapping("/chat")
@RestController
public class ChatClineController {
        @Autowired
        private  ChatClient chatClient;

        /*        public  ChatClineController (ChatClient.Builder chatClientBuilder) {
            this.chatClient = chatClientBuilder
                    //设置系统提示词
                    .defaultSystem("你叫梦，是由寻梦缘6研发的一款智能AI助手，你擅长Java，主要负责Java答疑，请以友好的态度来回答问题")
                    .build();
        }*/

        @GetMapping("/cline")
        String generation(String userInput) {
            return this.chatClient.prompt()
                    //用户输入
                    .user(userInput)
                    //日志打印，只对当前接口生效
                    .advisors(new SimpleLoggerAdvisor())
                    //调用大模型
                    .call()
                    //返回响应
                    .content();
        }

        record Recipe(String dish, List<String> ingredients){}

        @RequestMapping("/entity")
        public String entity(String userInput){
                Recipe entity = chatClient.prompt()
                        .user(String.format("请帮我生成%s的食谱", userInput))
                        .call()
                        .entity(Recipe.class);
                return entity.toString();
        }

        @GetMapping(value = "/stream",produces = "text/html;charset=utf-8")
        public Flux<String> stream(String userInput) {
                return this.chatClient.prompt()
                        //用户输入
                        .user(userInput)
                        //调用大模型流式返回
                        .stream()
                        //返回响应
                        .content();
        }
}
